package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appwxRespActionDao")
public class AppwxRespActionDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppwxRespAction queryWithKeys(String id) throws SQLException {
		AppwxRespAction appwxRespAction = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM  wx_resp_action WHERE wx_resp_actionid=?");
			ps.setString(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				appwxRespAction = new AppwxRespAction();
								appwxRespAction.setWx_resp_actionid(rs.getString("wx_resp_actionid"));
								appwxRespAction.setWx_resp_type(rs.getString("wx_resp_type"));
								appwxRespAction.setWx_resp_text(rs.getString("wx_resp_text"));
								appwxRespAction.setApp_id(rs.getString("app_id"));
							}
		} catch (SQLException e) {
			throw new SQLException("query wx_resp_action WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appwxRespAction;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppwxRespAction appwxRespAction) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into wx_resp_action( wx_resp_actionid , wx_resp_type , wx_resp_text , app_id) values(? , ? , ? , ? )";
			ps = conn.prepareStatement(sql);
						ps.setString(1, appwxRespAction.getWx_resp_actionid());
                        			ps.setString(2, appwxRespAction.getWx_resp_type());
                        			ps.setString(3, appwxRespAction.getWx_resp_text());
                        			ps.setString(4, appwxRespAction.getApp_id());
                        			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert wx_resp_action is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppwxRespAction appwxRespAction) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update wx_resp_action set wx_resp_type=?, wx_resp_text=?,  app_id=? where wx_resp_actionid=?";
			ps = conn.prepareStatement(sql);
	        			ps.setString(1, appwxRespAction.getWx_resp_type());
	        			ps.setString(2, appwxRespAction.getWx_resp_text());
	        			ps.setString(3, appwxRespAction.getApp_id());
	        			ps.setString(4, appwxRespAction.getWx_resp_actionid());
	        			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update wx_resp_action WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String recordKey) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from wx_resp_action where wx_resp_actionid=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, recordKey);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete wx_resp_action WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
